package com.jsbj.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsbj.base.ResultBean;
import com.jsbj.user.entities.SysRegisterUser;
import com.jsbj.user.mapper.SysRegisterUserMapper;
import com.jsbj.user.service.ISysRegisterUserService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 系统注册用户表 服务实现类
 * </p>
 *
 * @author jsbj
 * @since 2022-03-14
 */
@Service
public class SysRegisterUserServiceImpl extends ServiceImpl<SysRegisterUserMapper, SysRegisterUser> implements ISysRegisterUserService {

    @Override
    public int updateMessage(String phoneNumber, String message) {
        SysRegisterUser insertUser = new SysRegisterUser();
        insertUser.setMobile(phoneNumber);
        insertUser.setMessage(message);
        insertUser.setMessageTime(new Date());
        return baseMapper.insert(insertUser);
    }

    @Override
    public ResultBean checkMessage(String phoneNumber, String message) {
        ResultBean ret = new ResultBean();

        LambdaQueryWrapper<SysRegisterUser> wrapper = new LambdaQueryWrapper();
        wrapper.eq(SysRegisterUser::getMobile, phoneNumber);
        wrapper.eq(SysRegisterUser::getMessage, message);
        wrapper.orderByDesc(SysRegisterUser::getMessageTime);
        wrapper.last(" limit 1 ");
        SysRegisterUser sysRegisterUser = baseMapper.selectOne(wrapper);
        if (null == sysRegisterUser) {
            return ret.build("验证码错误！");
        }
        long d1 = new Date().getTime();
        long d2 = sysRegisterUser.getMessageTime().getTime();
        long diff = (d1 - d2) / 1000;
        if (diff > 600) {
            return ret.build("验证码已经超时，请重新发送！");
        }
        return ret.success("验证成功！");
    }
}
